<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
 
<div>计数：<span id="res"> </span></div>
<button onclick="startWorker()">开始工作</button> 
<button onclick="stopWorker()">停止工作</button>
 
<script>
    let worker;
    function startWorker() {
        worker = new Worker('./index.js');
        worker.onmessage = function(event) {
            console.log(event)
            document.querySelector('#res').textContent = event.data.num;
        }
    }
    
    function stopWorker()   { 
        worker.terminate();
    }
</script>
 
</body>
</html>
